package be.vdab.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import be.vdab.entities.Galgje;

/**
 * Servlet implementation class SausenRadenServlet
 */
@WebServlet("/sausenRaden.htm")
public class SausenRadenServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String VIEW = "WEB-INF/JSP/sausenRaden.jsp";
	private static final String REDIRECT_URL = "/sausenRaden.htm";

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {	
			HttpSession session = request.getSession(false);
			try {
		if (session==null) {
			session = request.getSession();
			Galgje spel = new Galgje();
			session.setAttribute("spel", spel);
			request.setAttribute("teTonenSaus", spel.getTeTonenSaus());
		}
		else {
			Galgje spel = (Galgje)session.getAttribute("spel");
			request.setAttribute("teTonenSaus", spel.getTeTonenSaus()); 
			}
			}catch (Exception ex) {
				request.setAttribute("Fout", ex.getMessage());
			}
		RequestDispatcher dispatcher = request.getRequestDispatcher(VIEW);
		dispatcher.forward(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		try {
		if (request.getParameter("letter").length()==1&&!request.getParameter("letter").matches(".*\\d.*")) {
			String gok = request.getParameter("letter").toLowerCase();
			Galgje spel = (Galgje)session.getAttribute("spel");
			spel.zoekLetter(gok);
			if (spel.getFoutePogingen()>=10) {
				String gefaald = ("U bent verloren, de saus was "+spel.getTeRadenSaus()+".");
				session.setAttribute("einde", gefaald);
			}
			session.setAttribute("teTonenSaus", spel.getTeTonenSaus());
			session.setAttribute("foutePogingen", Integer.toString(spel.getFoutePogingen()));
			if (spel.getTeRadenSaus().equalsIgnoreCase(spel.getTeTonenSaus())) {
				String gelukt = ("U bent gewonnen, de saus is "+spel.getTeRadenSaus()+".");
				session.setAttribute("einde", gelukt);
			}
		} else {
			String fout = "verkeerde invoer";
			session.setAttribute("fout", fout);
			}
		} catch (Exception ex) {
			session.setAttribute("fout", ex.getMessage());
		}
		
		try {
		if (request.getParameter("opnieuw").equals("Nieuw spel")) {
			Galgje spel = new Galgje();
			session.setAttribute("spel", spel);
			session.setAttribute("einde", null);
			session.setAttribute("foutePogingen", 0);
		}
		} catch (Exception ex) { session.setAttribute("fout", ex.getMessage());}
		
		response.sendRedirect(response.encodeRedirectURL(request
				.getContextPath() + REDIRECT_URL));
		

	}

}
